package com.cyclingAssistant.project;

import org.osmdroid.tileprovider.tilesource.TileSourceFactory;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;

public class TabMenu extends TabActivity {

	public static TabHost tabHost;
	public TabWidget tabWidget;
	public View layerTab;

	public PopupWindow pw;
	Boolean clicked = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_menu);

		Resources res = getResources(); // Resource object to get Drawables
		tabHost = getTabHost(); // The activity TabHost

		tabWidget = tabHost.getTabWidget();

		TabHost.TabSpec spec; // Resusable TabSpec for each tab
		Intent intent; // Reusable Intent for each tab
 
		// Create an Intent to launch an Activity for the tab (to be reused)
		intent = new Intent().setClass(this, CyclingAssistantActivity.class);

		// Initialize a TabSpec for each tab and add it to the TabHost
		spec = tabHost.newTabSpec("main")
				.setIndicator("", res.getDrawable(R.drawable.main))
				.setContent(intent);
		tabHost.addTab(spec);

		// Do the same for the other tabs
		intent = new Intent().setClass(this, RoutePlan.class);
		spec = tabHost.newTabSpec("plan route")
				.setIndicator("", res.getDrawable(R.drawable.directions))
				.setContent(intent);
		tabHost.addTab(spec);

		spec = tabHost.newTabSpec("layers")
				.setIndicator("", res.getDrawable(R.drawable.layers))
				.setContent(R.id.layout_layers);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, Options.class);
		spec = tabHost.newTabSpec("optionss")
				.setIndicator("", res.getDrawable(R.drawable.settings))
				.setContent(intent);
		tabHost.addTab(spec);

		layerTab = tabWidget.getChildAt(2);
		layerTab.setOnClickListener(new ShowLayerOptions());

		tabHost.setCurrentTab(0);

		tabHost.setOnTabChangedListener(new OnTabChangeListener() {

			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				if (pw != null ) {
					pw.dismiss();
					pw = null;
				}
			}
		});
	}

	class ShowLayerOptions implements android.view.View.OnClickListener {


		@Override
		public void onClick(View v) {
			if (!clicked) {
				showPopupWindow(TabMenu.this, tabHost.getTabContentView());// findViewById(android.R.id.content));//
				clicked = true;
			} else {
				if (pw != null ) {
					pw.dismiss();
					pw = null;
				}
				clicked = false;
			}
			
		}

		public void showPopupWindow(Context context, View parent) {
			final View vPopupWindow = TabMenu.this.getLayoutInflater().inflate(
					R.layout.layers_selection, null); // tabHost.getTabContentView(),true);
			pw = new PopupWindow(vPopupWindow, LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
			// pw.setFocusable(true);
			// pw.showAsDropDown(parent, 30, 30);
			pw.showAtLocation(parent, Gravity.CENTER_VERTICAL, 0, 255);
			// pw.showAsDropDown(parent);

			ImageButton incline = (ImageButton) vPopupWindow
					.findViewById(R.id.button_incline_layer);
			incline.setOnClickListener(new android.view.View.OnClickListener() {
				@Override
				public void onClick(View v) {
					CyclingAssistantActivity.mapView
							.setTileSource(TileSourceFactory.MAPNIK);
					tabHost.setCurrentTab(0);
					if (pw != null && pw.isShowing()) {
						pw.dismiss();
						pw = null;
					}
				}
			});

			ImageButton scene = (ImageButton) vPopupWindow
					.findViewById(R.id.button_scene_layer);
			scene.setOnClickListener(new android.view.View.OnClickListener() {
				@Override
				public void onClick(View v) {
					CyclingAssistantActivity.mapView
							.setTileSource(TileSourceFactory.MAPQUESTOSM);
					tabHost.setCurrentTab(0);
					if (pw != null && pw.isShowing()) {
						pw.dismiss();
						pw = null;
					}
				}
			});

			ImageButton pollution = (ImageButton) vPopupWindow
					.findViewById(R.id.button_pollution_layer);
			pollution
					.setOnClickListener(new android.view.View.OnClickListener() {
						@Override
						public void onClick(View v) {
							CyclingAssistantActivity.mapView
									.setTileSource(TileSourceFactory.CLOUDMADESTANDARDTILES);
							tabHost.setCurrentTab(0);
							if (pw != null && pw.isShowing()) {
								pw.dismiss();
								pw = null;
							}
						}
					});

			ImageButton designated = (ImageButton) vPopupWindow
					.findViewById(R.id.button_designated_layer);
			designated
					.setOnClickListener(new android.view.View.OnClickListener() {
						@Override
						public void onClick(View v) {
							CyclingAssistantActivity.mapView
									.setTileSource(TileSourceFactory.CYCLEMAP);
							tabHost.setCurrentTab(0);
							if (pw != null && pw.isShowing()) {
								pw.dismiss();
								pw = null;
							}
						}
					});

		}
	}

}
